package com.bl.weilan.service.biz.companypoint.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.CompanyPointEntity;
import com.bl.weilan.dao.repository.CompanyPointRepository;
import com.bl.weilan.service.biz.companypoint.CompanyPointService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.companypoint.CompanyPointAddVo;
import com.bl.weilan.service.vo.companypoint.CompanyPointListVo;
import com.bl.weilan.service.vo.companypoint.CompanyPointModVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Log4j2
public class CompanyPointServiceImpl implements CompanyPointService {
    @Autowired
    CompanyPointRepository companypointRepository;

//    @Override
//    public CompanyPointEntity getCompanyPointById(String companyId) {
//        CompanyPointEntity companypointEntity = companypointRepository.findByCompanyId(companyId);
//        return companypointEntity;
//    }

    @Override
    public CompanyPointListVo getCompanyPointLists(Pageable pageable ){
        Page<CompanyPointEntity> companypointEntities;
        CompanyPointListVo companypointListVo = new CompanyPointListVo();

        companypointEntities = companypointRepository.findAll(pageable);


        companypointListVo.setTotalCount(companypointEntities.getTotalElements());
        companypointListVo.setList(companypointEntities.getContent());

        return companypointListVo;
    }

    @Override
    public void addCompanyPoint(CompanyPointAddVo companypointAddVo) {
        CompanyPointEntity companypointEntity = new CompanyPointEntity();
        companypointEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(companypointAddVo, companypointEntity);
        companypointRepository.save(companypointEntity);

    }

    @Override
    public void del(DelBody id) {
        companypointRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            companypointRepository.delete(id);
        }
    }

    @Override
    public void modify(CompanyPointModVo companypointModVo){
        CompanyPointEntity companypointEntity = companypointRepository.findOne(companypointModVo.getId());

        if (companypointEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        BeanUtils.copyProperties(companypointModVo, companypointEntity);
        companypointRepository.save(companypointEntity);
    }
}
